$(function () {
  //查询购物车接口，id为用户id
  var Car = function () {
    axios
      .get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
        params: { id: 74651 },
      })
      .then(function (res) {
        var data = res.data.data;
        let str = "";
        $.each(data, function (i, ele) {
          var a = ele.pnum * ele.pprice; //商品总价
          str += `
            <div class="good_li" data-id="${ele.pid}">
              <input type="checkbox" class="dan">
              <span class="del">删除</span>
              <div class="pic"><img src="${ele.pimg}" alt=""></div>
              <div class="pname">${ele.pname}</div>  
              <span class="money">￥</span>                  
              <div class="jiage">${ele.pprice}</div>
              <div class="number">
                <span class="plus">+</span>
                <span class="num">${ele.pnum}</span>
                <span class="substract">-</span>
              </div> 
              <span class="money">￥</span>  
              <div class="jiage_zong">${a}</div>   
            </div>    
            `;
        });
        $(".car_con").append(str);
      })
      .then(function () {
        var uid = 74651;
        //删除购物车
        $(".del").on("click", function () {
          console.log("d");
          let c = Number($(this).parents(".good_li").attr("data-id"));
          axios.delete("http://jx.xuzhixiang.top/ap/api/cart-delete.php", {
            params: { uid, pid: c },
          });
          window.location.reload();
        });
        //增加减少购物车数据
        $(".plus").on("click", function () {
          var n = $(this).siblings(".num").text();
          var b = Number($(this).parent().siblings(".jiage").text());
          n++;
          var zong = (n * b).toFixed(2);
          $(this).siblings(".num").text(n);
          $(this).parent().siblings(".jiage_zong").text(zong);
          var c = Number($(this).parents(".good_li").attr("data-id")); //获取当前商品id

          //更改购物车中的数据
          axios.get("http://jx.xuzhixiang.top/ap/api/cart-update-num.php", {
            params: { uid, pid: c, pnum: n },
          });
        });
        $(".substract").on("click", function () {
          var n = $(this).siblings(".num").text();
          var b = Number($(this).parent().siblings(".jiage").text());
          if (n <= 1) {
            return;
          }
          n--;
          $(this).siblings(".num").text(n);
          var zong = (n * b).toFixed(2);
          $(this).parent().siblings(".jiage_zong").text(zong);
          var c = Number($(this).parents(".good_li").attr("data-id"));
          axios.get("http://jx.xuzhixiang.top/ap/api/cart-update-num.php", {
            params: { uid, pid: c, pnum: n },
          });
        });
      })
      //全选单选按钮控制
      .then(function () {
        let str = "";
        $(".dan").on("click", function () {
          if ($(".dan:checked").length == $(".dan").length) {
            $(".all").prop("checked", true);
          } else {
            $(".all").prop("checked", false);
          }
          //将选中的商品渲染到代买项
          let n = 0;
          if ($(this).prop("checked") == true) {
            let m = $(this).parent().siblings(".buy").find(".chose").text();
            let n = $(this).parent().siblings(".buy").find(".p").text();
            let jiage = $(this).siblings(".jiage_zong").text();
            let b = Number($(this).siblings(".jiage").text());
            console.log(b);
            jiage = Number(m) + Number(jiage);
            jiage = jiage.toFixed(2);
            let num = $(this).siblings(".number").find(".num").text();
            num = Number(n) + Number(num);
            // --------------------------
            $(this)
              .siblings(".number")
              .children(".plus")
              .on("click", function () {
                ++num;
                jiage = Number(b) + Number(jiage);
                str = `
                <span>￥</span>            
                <div class="chose">${jiage}</div>
                <div class="p">${num}</div>            
                `;
                $(".buy_con").empty().append(str);
              });
            // --------------
            $(this)
              .siblings(".number")
              .children(".substract")
              .on("click", function () {
                --num;
                console.log(num);
                jiage = Number(jiage) - Number(b);
                str = `
                <span>￥</span>            
                <div class="chose">${jiage}</div>
                <div class="p">${num}</div>            
                `;
                $(".buy_con").empty().append(str);
              });
            // ---------------
            str = `
              <span>￥</span>            
              <div class="chose">${jiage}</div>
              <div class="p">${num}</div>            
              `;
            console.log(num);
          } else {
            let m = $(this).parent().siblings(".buy").find(".chose").text();
            let n = $(this).parent().siblings(".buy").find(".p").text();
            let jiage = $(this).siblings(".jiage_zong").text();
            jiage = Number(m) - Number(jiage);
            jiage = jiage.toFixed(2);
            jiage <= 0 ? 0 : jiage;
            let num = $(this).siblings(".number").find(".num").text();
            num = Number(n) - Number(num);
            num <= 0 ? 0 : num;
            str = `
            <span>￥</span>
            <div class="chose">${jiage}</div>
            <div class="p">${num}</div>            
            `;
          }
          $(".buy_con").empty().append(str);
        }); //全选按钮渲染
        $(".all").on("click", function () {
          let a = $(".all").prop("checked");
          $(".dan").prop("checked", a);
          if (a == true) {
            axios
              .get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
                params: { id: 74651 },
              })
              .then(function (res) {
                let data = res.data.data;
                console.log(res);
                let n = 0;
                let p = 0;
                $(data).each(function (i, ele) {
                  n += Number(ele.pnum);
                  p += Number(ele.pnum) * Number(ele.pprice);
                  // p = p.toFixed(2);
                });

                str = `  
                <span>￥</span>          
                <div class="chose">${p}</div>
                <div class="p">${n}</div>            
                `;
                $(".buy_con").empty().append(str);
              });
          } else {
            str = `  
            <span>￥</span>          
            <div class="chose">${0}</div>
            <div class="p">${0}</div>            
            `;
            $(".buy_con").empty().append(str);
          }
        });
      });
  };
  Car();
})();
